﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
#if NET46
using Newtonsoft.Json;
#elif NET47
using Newtonsoft.Json;
#elif NET48
using Newtonsoft.Json;
#elif NETCOREAPP2_0
using System.Text.Json;
using System.Text.Json.Serialization;
#elif NETCOREAPP2_2
using System.Text.Json;
using System.Text.Json.Serialization;
#elif NETCOREAPP3_0
using System.Text.Json;
using System.Text.Json.Serialization;
#elif NETSTANDARD2_0
using System.Text.Json;
using System.Text.Json.Serialization;
#elif NETSTANDARD2_1
using System.Text.Json;
using System.Text.Json.Serialization;
#endif

namespace LyyCommon.Helper
{
    public class JsonHelper
    {
#region

        /// <summary> 
        /// 对象转JSON (.net46到.net standard2.0)
        /// </summary> 
        /// <param name="obj">需要进行序列号的对象</param> 
        /// <returns>JSON格式的字符串</returns> 
        public static string ObjectToJSON(object obj,bool isutf8=true)
        {
            string json = "";
#if NET46
            json = JsonConvert.SerializeObject(obj);
#elif NET47
            json = JsonConvert.SerializeObject(obj);
#elif NET48
            json = JsonConvert.SerializeObject(obj);
#elif NETCOREAPP2_0
            if (isutf8)
            {
                byte[] utf8Json = JsonSerializer.SerializeToUtf8Bytes(obj);
                json = System.Text.Encoding.UTF8.GetString(utf8Json);
            }
            else
            {
                json = JsonSerializer.Serialize(obj);
            }
#elif NETCOREAPP2_2
            if (isutf8)
            {
                byte[] utf8Json = JsonSerializer.SerializeToUtf8Bytes(obj);
                json = System.Text.Encoding.UTF8.GetString(utf8Json);
            }
            else
            {
                json = JsonSerializer.Serialize(obj);
            }
#elif NETCOREAPP3_0
            if (isutf8)
            {
                byte[] utf8Json = JsonSerializer.SerializeToUtf8Bytes(obj);
                json = System.Text.Encoding.UTF8.GetString(utf8Json);
            }
            else
            {
                json = JsonSerializer.Serialize(obj);
            }
#elif NETSTANDARD2_0
            if (isutf8)
            {
                byte[] utf8Json = JsonSerializer.SerializeToUtf8Bytes(obj);
                //json = System.Text.Encoding.Default.GetString(utf8Json);
                json = System.Text.Encoding.UTF8.GetString(utf8Json);
            }
            else
            {
                json = JsonSerializer.Serialize(obj);
            }
#elif NETSTANDARD2_1
            if (isutf8)
            {
                byte[] utf8Json = JsonSerializer.SerializeToUtf8Bytes(obj);
                json = System.Text.Encoding.UTF8.GetString(utf8Json);
            }
            else
            {
                json = JsonSerializer.Serialize(obj);
            }
#endif
            return json;

        }


        /// <summary> 
        /// JSON文本转对象,泛型方法 (.net46到.net standard2.0)
        /// </summary> 
        /// <typeparam name="T">类型</typeparam> 
        /// <param name="jsonText">JSON文本</param> 
        /// <returns>指定类型的对象</returns> 
        public static T JSONToObject<T>(string jsonText)
        {
#if NET46
            try
            {
                return JsonConvert.DeserializeObject<T>(jsonText);
            }
            catch (global::System.Exception ex)
            {
                return default(T);
            }
#elif NET47
            try
            {
                return JsonConvert.DeserializeObject<T>(jsonText);
            }
            catch (global::System.Exception ex)
            {
                return default(T);
            }
#elif NET48
            try
            {
                return JsonConvert.DeserializeObject<T>(jsonText);
            }
            catch (global::System.Exception ex)
            {
                return default(T);
            }
#elif NETCOREAPP2_0
            return JsonSerializer.Deserialize<T>(jsonText);
#elif NETCOREAPP2_2
            return JsonSerializer.Deserialize<T>(jsonText);
#elif NETCOREAPP3_0
             return JsonSerializer.Deserialize<T>(jsonText);
#elif NETSTANDARD2_0
            return JsonSerializer.Deserialize<T>(jsonText);
#elif NETSTANDARD2_1
            return JsonSerializer.Deserialize<T>(jsonText);
#endif
        }

        /// <summary> 
        /// JSON文本转对象,泛型方法(不区分版本)
        /// </summary> 
        /// <typeparam name="T">类型</typeparam> 
        /// <param name="jsonText">JSON文本</param> 
        /// <returns>指定类型的对象</returns> 
        public static object JSONToObject(string jsonText)
        {
            try
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject(jsonText);
            }
            catch (global::System.Exception ex)
            {
                return null;
            }
        }


        /// <summary> 
        /// 数据表转键值对集合
        /// 把DataTable转成 List集合, 存每一行 
        /// 集合中放的是键值对字典,存每一列 
        /// </summary> 
        /// <param name="dt">数据表</param> 
        /// <returns>哈希表数组</returns> 
        public static List<Dictionary<string, object>> DataTableToList(DataTable dt)
        {
            List<Dictionary<string, object>> list
                 = new List<Dictionary<string, object>>();

            foreach (DataRow dr in dt.Rows)
            {
                Dictionary<string, object> dic = new Dictionary<string, object>();
                foreach (DataColumn dc in dt.Columns)
                {
                    dic.Add(dc.ColumnName, dr[dc.ColumnName]);
                }
                list.Add(dic);
            }
            return list;
        }

        /// <summary> 
        /// 数据集转键值对数组字典 
        /// </summary> 
        /// <param name="dataSet">数据集</param> 
        /// <returns>键值对数组字典</returns> 
        public static Dictionary<string, List<Dictionary<string, object>>> DataSetToDic(DataSet ds)
        {
            Dictionary<string, List<Dictionary<string, object>>> result = new Dictionary<string, List<Dictionary<string, object>>>();

            foreach (DataTable dt in ds.Tables)
                result.Add(dt.TableName, DataTableToList(dt));

            return result;
        }

        /// <summary> 
        /// 数据表转JSON 
        /// </summary> 
        /// <param name="dataTable">数据表</param> 
        /// <returns>JSON字符串</returns> 
        public static string DataTableToJSON(DataTable dt)
        {
            return ObjectToJSON(DataTableToList(dt));
        }

       

        /// <summary> 
        /// 将JSON文本转换为数据表数据 
        /// </summary> 
        /// <param name="jsonText">JSON文本</param> 
        /// <returns>数据表字典</returns> 
        public static Dictionary<string, List<Dictionary<string, object>>> TablesDataFromJSON(string jsonText)
        {
            return JSONToObject<Dictionary<string, List<Dictionary<string, object>>>>(jsonText);
        }

        /// <summary> 
        /// 将JSON文本转换成数据行 
        /// </summary> 
        /// <param name="jsonText">JSON文本</param> 
        /// <returns>数据行的字典</returns>
        public static Dictionary<string, object> DataRowFromJSON(string jsonText)
        {
            return JSONToObject<Dictionary<string, object>>(jsonText);
        }

#endregion

    }
}
